package com.example.JavaWebApi.service.impl.neo4j;

import com.example.JavaWebApi.core.neo4j.Neo4jHelper;
import com.example.JavaWebApi.mapper.neo4j.MovieRepository;
import com.example.JavaWebApi.mapper.neo4j.PersonRepository;
import com.example.JavaWebApi.model.neo4j.node.Movie;
import com.example.JavaWebApi.model.neo4j.node.Person;
import com.example.JavaWebApi.model.neo4j.relation.Relation;
import com.example.JavaWebApi.model.neo4j.result.RelationVo;
import com.example.JavaWebApi.service.neo4j.IMovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @program: java-web-api
 * @description:
 * @author: 杨泽
 * @create: 2023-09-13 11:36
 */
@Service
public class MovieServiceImpl implements IMovieService {
    @Autowired
    private MovieRepository movieRepository;
    @Autowired
    private PersonRepository personRepository;
    @Autowired
    private Neo4jHelper neo4jHelper;
    @Override
    public Boolean add(Movie movie) {
        movieRepository.save(movie);
        return true;
    }

    @Override
    public List<Movie> getAll() {
        movieRepository.deleteAll();
        return movieRepository.findAll();
    }

    @Override
    public Boolean createRelation(RelationVo relationVo) {
        Optional<Movie> movie=movieRepository.findById(relationVo.getStartId());
        Optional<Person> person=personRepository.findById(relationVo.getEndId());
        if(movie.isPresent()&&person.isPresent()){
            Movie m=movie.get();
            Relation relation=new Relation();
            List<String> arr=new ArrayList<>();
            arr.add(relationVo.getRelation());
            relation.setPerson(person.get());
            m.getActors().add(relation);
            movieRepository.save(m);
            return true;
        }
        return false;
    }

    @Override
    public Boolean createRelation1(RelationVo relationVo) {
        //查询
        Movie movie=new Movie();
        movie.setName("盗梦");
        ExampleMatcher matcher=ExampleMatcher
                .matching()
                .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());
        Sort sort=Sort.by(Sort.Direction.DESC,"name");
        Example example=Example.of(movie,matcher);
        List<Movie> list= movieRepository.findAll(example,sort);
        //新增或者修改
        movieRepository.save(movie);
        //删除
        movieRepository.delete(movie);
        movieRepository.deleteById(movie.getId());
        return true;
    }
}